package com.dao;

import java.util.List;

import com.beans.AdminInfo;
import com.beans.CateInfo;
import com.beans.GoodInfo;
import com.beans.MenuInfo;
import com.beans.PageInfo;
import com.commons.Enums;
import com.jdbc.DBUtil;

public class GoodsDao {

	public void addParents(String catName, String des,int parentId) {
		DBUtil.update("insert into cateinfo (cateName,des,parentId) values (?,?,?)",catName,des,parentId);
	}

	public CateInfo checkParentName(String cateName) {
		return  DBUtil.getSingleObj("select * from cateinfo where cateName = ?", CateInfo.class, cateName);
	}

	public List<CateInfo> selectParents() {
		return DBUtil.getList("select * from cateinfo where parentId = 0", CateInfo.class);
	}

	public CateInfo checkSonName(int parentId, String cateName) {
		return DBUtil.getSingleObj("select * from cateinfo where (parentId = 0 or parentId = ?) and cateName = ?", CateInfo.class, parentId,cateName);
	}

	public List<CateInfo> managelist(int parentId) {
		List<CateInfo> catelist = DBUtil.getList("select * from cateinfo where parentId=?", CateInfo.class, parentId);
				for(CateInfo m:catelist) {
					if(m.getParentid()==0) {
						m.setSubCateList(managelist(m.getId()));
					}
				}
				return catelist;
			}

	public void deletecate(int id) {
		DBUtil.update("delete from cateinfo where id=?", id);
	}

	public CateInfo selectByid(int id) {
		return DBUtil.getSingleObj("select * from cateinfo where id = ?",CateInfo.class, id);
	}

	public void updatebig(int id, String cateName, String des) {
		DBUtil.update("update cateinfo set cateName = ?,des = ? where id = ?", cateName,des,id);
	}

	public void updatesmall(int id, String cateName, String des, int parentid) {
		DBUtil.update("update cateinfo set cateName = ?,des = ?,parentid=? where id = ?", cateName,des,parentid,id);
		
	}

	public int addGoods(GoodInfo good) {
		return DBUtil.addWithId("insert into goodsinfo (goodsName,bigCateId,smallCateId,price,des,unit,producter,pictureData) values(?,?,?,?,?,?,?,?)", 
				good.getGoodsName(),good.getBigCateId(),good.getSmallCateId(),good.getPrice(),good.getDes(),good.getUnit(),good.getProducter(),good.getPictureData());
	}

	public List<CateInfo> selectSons(int bigcateid) {
		return DBUtil.getList("select * from cateinfo where parentid = ?", CateInfo.class, bigcateid);
		
	}

	public GoodInfo getGoodById(int goodsId) {
		return DBUtil.getSingleObj("select * from goodsinfo where id=?", GoodInfo.class, goodsId);
	}

	public GoodInfo textgoodsName(String goodsName) {
		return DBUtil.getSingleObj("select * from goodsinfo where goodsName = ?",GoodInfo.class,goodsName);
	}

	public int number(String sql) {
		return Integer.parseInt(DBUtil.getScalar(sql)+"");
	}

	public List<GoodInfo> page(PageInfo page, String sql) {
		return DBUtil.getList(sql,GoodInfo.class,page.getBeginRow(),page.getPageSize());

	}

	public void deleteGoodsById(int id) {
		DBUtil.update("delete from goodsinfo where id=?", id);
	}

	public void updategoods(GoodInfo good) {
		DBUtil.update("update goodsinfo set goodsName=?,bigCateId=?,smallCateId=?,price=?,des=?,unit=?,producter=?,pictureData=? where id=?",
				good.getGoodsName(),good.getBigCateId(),good.getSmallCateId(),good.getPrice(),good.getDes(),good.getUnit(),good.getProducter(),good.getPictureData()
				,good.getId());
	}

	public void updategoodswithoutPicture(GoodInfo good) {
		DBUtil.update("update goodsinfo set goodsName=?,bigCateId=?,smallCateId=?,price=?,des=?,unit=?,producter=? where id=?",
				good.getGoodsName(),good.getBigCateId(),good.getSmallCateId(),good.getPrice(),good.getDes(),good.getUnit(),good.getProducter()
				,good.getId());
	}

	public void delAdminByIdList(String[] ids) {
		String sql="delete from goodsinfo where id in (";
		for(int i=0;i<ids.length;i++) {
			if(i!=ids.length-1) {
				sql=sql+ids[i]+",";
			}else {
				sql=sql+ids[i]+")";
			}	
		}
		DBUtil.update(sql);
	}
	
}
